using SharedKernel.Infrastructure.Repositories;
using SqlSugar;
using System;
using System.Threading.Tasks; 
using UserService.Domain.Models;
using UserService.Domain.Repositories;
using UserService.Domain.ValueObjects;

namespace UserService.Infrastructure.Repositories
{
    /// <summary>
    /// 用户仓储实现
    /// </summary>
    public class UserRepository : SqlSugarRepository<User, long>, IUserRepository
    {
        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="serviceProvider">服务提供器</param>
        /// <param name="context">SqlSugar客户端</param>
        public UserRepository(IServiceProvider serviceProvider, ISqlSugarClient context) : base(serviceProvider, context)
        { }

        /// <summary>
        /// 根据id获取用户
        /// </summary>
        /// <param name="userId">用户id</param>
        /// <returns>用户实体</returns>
        public async Task<User> GetUserById(long userId)
        {
            return await Entities.FirstAsync(u => u.Id == userId);
        }

        /// <summary>
        /// 根据用户名获取用户
        /// </summary>
        /// <param name="username">用户名</param>
        /// <returns>用户实体</returns>
        public async Task<User> GetByUsernameAsync(string username)
        {
            return await Entities.FirstAsync(u => u.Name == username);
        }

        /// <summary>
        /// 根据电子邮件获取用户
        /// </summary>
        /// <param name="email">电子邮件</param>
        /// <returns>用户实体</returns>
        public async Task<User> GetByEmailAsync(string email)
        {
            return await Entities.FirstAsync(u => u.Email == email);
        }

        /// <summary>
        /// 用户登录
        /// </summary>
        /// <param name="dto">dto</param>
        /// <returns>用户实体</returns>
        public async Task<User> GetUserLogin(UserLoginValueObject dto)
        {
            return await Entities.FirstAsync(t => t.Phone == dto.Phone && t.Password == dto.Password);
        }

    }
}